fix: lazy-load MCP SDK to avoid global state pollution in Next.js#141
Open
mr-kelly wants to merge 1 commit intovercel:mainfrom
Open
fix: lazy-load MCP SDK to avoid global state pollution in Next.js#141mr-kelly wants to merge 1 commit intovercel:mainfrom
mr-kelly wants to merge 1 commit intovercel:mainfrom
Conversation
The @modelcontextprotocol/sdk has dependencies (via undici) that modify the global Response object at import time. This breaks Next.js App Routes which rely on instanceof Response checks, causing "No response is returned" errors. This fix: - Converts eager SDK imports to type-only imports - Adds a lazy loadSdk() function that dynamically imports SDK modules - Defers StreamableHTTPServerTransport creation to first request - Calls loadSdk() before SDK usage in both HTTP and SSE endpoints This ensures the SDK is only loaded after Next.js has fully initialized. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
Hello all, this issue is impacting my project as well. Using Mr Kelly's patch is working. Is there a timeline for incorporating a fix for this issue into the vercel mcp-handler project? |
br-schneider
added a commit
to br-schneider/mcp-handler
that referenced
this pull request
Mar 4, 2026
The @modelcontextprotocol/sdk has transitive dependencies (via undici) that replace globalThis.Response when imported. This breaks Next.js App Router route handlers which validate responses with instanceof Response, causing "No response is returned from route handler" errors for ALL routes in the same process — not just the MCP endpoint. Lazy-loading alone (as proposed in vercel#141) only delays the problem. Once the MCP endpoint is hit, the global Response is replaced and all subsequent requests to any route handler fail. This fix: - Converts eager SDK imports to type-only imports - Adds a loadSdk() function that dynamically imports SDK modules - Saves globalThis.Response before import, restores it after - Defers StreamableHTTPServerTransport creation to first request - Calls loadSdk() at the start of mcpApiHandler Fixes vercel#140
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The @modelcontextprotocol/sdk has dependencies (via undici) that modify the global Response object at import time. This breaks Next.js App Routes which rely on instanceof Response checks, causing "No response is returned" errors.
This fix:
This ensures the SDK is only loaded after Next.js has fully initialized.